package com.stoken.web.control;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.stoken.domain.LineUse;
import com.stoken.domain.MemberBaseInfo;
import com.stoken.domain.MemberShopWallet;
import com.stoken.domain.TravelLineType;
import com.stoken.domain.TypeLineInfo;
import com.stoken.service.LineUseService;
import com.stoken.service.MemberShopWalletService;
import com.stoken.service.TravelLineTypeService;
import com.stoken.service.TypeLineInfoService;
import com.stoken.web.StokenUtils;
import com.symbio.commons.Compositor;
import com.symbio.commons.Compositor.CompositorType;
import com.symbio.commons.Filtration;
import com.symbio.commons.Filtration.MatchType;
import com.symbio.commons.Page;
import com.symbio.utils.WebUtils;

@Controller
@RequestMapping("/travelLine")
public class TravelLineControl {
	
	@Autowired
	private TypeLineInfoService traveLineService;
	
	@Autowired
	private TravelLineTypeService travelLineTypeService;
	
	@Autowired
	private LineUseService lineUseService;
	
	@Autowired
	private MemberShopWalletService memberShopWalletService;
	
	/**
	 * 加载旅游线路类型信息
	 * @return
	 */
	@RequestMapping("/backstage/lineList")
	public String loadTraveLineList(ModelMap modeMape){
		List<TravelLineType> travelLineTypes = travelLineTypeService.findAll();
		modeMape.addAttribute("travelLineTypes", travelLineTypes);		
		return "/backstage/travel/travelLineList";		
	}
	
	@RequestMapping("/backstage/travelLineData")
	public String loadTraveLineData(HttpServletRequest request,ModelMap modeMape,String typeLable){
		Page<TypeLineInfo> pageData = new Page<TypeLineInfo>();
		//给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		Compositor compositor = new Compositor("id",CompositorType.DESC);
		pageData.setCompositor(compositor);
		if(typeLable.equals("-1000")){
			typeLable = "1";
		}
		
		pageData = traveLineService.find(pageData,typeLable);		
	    modeMape.addAttribute("pageData", pageData);
	    
	    Pagination page = new Pagination();
		page.setIntTotalNum((int)pageData.getPagination().getTotalCount());
		page.setCurpage(pageData.getPagination().getPageNo());
		page.setPagenum(pageData.getPagination().getPageSize());
		modeMape.addAttribute("pagination", page.showPages());
		return "/backstage/travel/travelLineData";		
	}
	
	/**
	 * 增加旅游线路
	 */
	@RequestMapping(value="/backstage/saveTravelLineAdd",method=RequestMethod.POST)
	public String addTravelType(TypeLineInfo typeLineInfo,HttpServletRequest request,ModelMap modeMape,String typeLabel){	
		TravelLineType travelLineType = travelLineTypeService.find("typeLabel", typeLabel);
		typeLineInfo.setLineType(travelLineType);
		typeLineInfo.setReleaseTime(new Date());
		traveLineService.save(typeLineInfo);		
		
		return "/backstage/travel/travelLineList";		
	}
	
	/**
	 * 删除旅游类型
	 * @param request
	 * @param modeMape
	 * @param id
	 * @return
	 */
	@RequestMapping("/backstage/deleteTravelLine")
	public String deleteType(HttpServletRequest request,ModelMap modeMape,Long travelLineId){		
		traveLineService.delete(travelLineId);
		return "/backstage/travel/travelLineList";		
	}
	
	
	/**
	 * 加载新增旅游类型
	 * @return
	 */
	@RequestMapping("/backstage/loadTravelLineAdd")
	public String loadAddTravelTypePage(ModelMap modeMape){		
		List<TravelLineType> travelLineTypes = travelLineTypeService.findAll();
		modeMape.addAttribute("travelLineTypes", travelLineTypes);
		return "/backstage/travel/addTravelLine";
		
	}
	
	/**
	 * 查看旅游路线首页
	 * @param modeMape
	 * @return
	 */
	@RequestMapping("/travelIndex")
	public String travelIndex(ModelMap modeMape,String typeLable){
		modeMape.addAttribute("typeLable",typeLable);
	    return "travel/travelList";
	}
	
	/**
	 * 旅游路线page
	 * @param modeMape
	 * @param request
	 * @param typeLable
	 * @return
	 */
	@RequestMapping("/travelList")
	public String travelList(ModelMap modeMape,HttpServletRequest request,String typeLable){	
		MemberBaseInfo mbi = (MemberBaseInfo) request.getSession().getAttribute(StokenUtils.LOGINACCOUNT);
		Page<TypeLineInfo> pageData = new Page<TypeLineInfo>();
		//给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		Compositor compositor = new Compositor("releaseTime",CompositorType.DESC);
		pageData.setCompositor(compositor);
		pageData = traveLineService.find(pageData,typeLable);		
	    modeMape.addAttribute("pageData", pageData);
	    
	    Pagination page = new Pagination();
		page.setIntTotalNum((int)pageData.getPagination().getTotalCount());
		page.setCurpage(pageData.getPagination().getPageNo());
		page.setPagenum(pageData.getPagination().getPageSize());
		//查询线路信息
		LineUse lineUse = lineUseService.getLineUseByBaseUserName(mbi.getBaseUsername());
		//查询用户消费基金
		MemberShopWallet memberShopWallet = memberShopWalletService.getShopWalletByBaseUserName(mbi.getBaseUsername());
	    request.setAttribute("memberShopWallet", memberShopWallet);
		request.setAttribute("lineUse", lineUse);
		modeMape.addAttribute("pagination", page.showPages());
		modeMape.addAttribute("typeLable",typeLable);
	    return "travel/travelPage";
	}

	
}
